/***************************************************************************************************
This file create the dataset prices.dta. 

This dataset includes the actual and counterfactual valuation ratio for each asset class
as well as the relative deviation (PD - \overline{PD}) / PD

***************************************************************************************************/

/***************************************************************************************************
Generate dataset with valuation ratios and their deviations relative to balanced growth path
***************************************************************************************************/
* create equity index for Norway
import delimited using "$MyPath/Data/worldscope/worldscope.csv", clear
keep if countrycode == 578
keep year cf_yield
rename cf_yield cf_yield_norway
gen PDe = 1 / cf_yield_norway
tempfile temp_pde_norway
save `temp_pde_norway'

* create equity index for Norway
* for some reason Brazil and Venezual have huge market value. I think it's due to conversion issues from local currenties to global currencies. Hence, drop these countries from the world index.
import delimited using "$MyPath/Data/worldscope/worldscope.csv", clear
drop if inlist(countrycode, 76, 862)
collapse (mean) cf_yield [w = asset_market], by(year)
rename cf_yield cf_yield_world
gen PDe_world = 1 / cf_yield_world
tempfile temp_pde_world
save `temp_pde_world'


* create housing price-to-rent ratio
* combine datasets on house price and rent index
import delimited using  "$MyPath/Data/Aggregate Data/nb_houseindices.csv", clear
tempfile temp
save `temp'
import delimited using "$MyPath/Data/Aggregate Data/Table03013.csv", clear
tsset year
merge 1:1 year using `temp', nogen
gen PDh = all / rentpricescpissb
tempfile temp_pdh
save `temp_pdh'

* create housing price-to-rent ratio from Jorda et al. database
use "$MyPath/Data/Aggregate Data/JSTdatasetR6", clear
keep if country == "Norway"
gen PDh_jst = 1 / housing_rent_yd
keep year PDh_jst
tempfile temp_pdh_jst
save `temp_pdh_jst'



import delimited using "$MyPath/Data/Aggregate Data/cpi.csv", clear
tsset year
gen inflation = cpi / L.cpi - 1
gen inflation_smoothed = (inflation + L.inflation + L2.inflation + L3.inflation + L4.inflation) /  5
tempfile infl
save `infl'

* Generate dataset with mortage and deposit rates in Norway
import excel year = A mortgagerate_nominal = C depositrate_nominal = E using "$MyPath/Data/Aggregate Data/Table08175.xlsx", clear cellrange(A4:E45)
destring *, replace
gen Rd = depositrate_nominal / 100
gen Rm = mortgagerate_nominal / 100
merge 1:1 year using `infl', keep(master matched) nogen
tsset year
gen Qd = 1 / ((1 + F.Rd) / (1 + F.inflation_smoothed))
gen Qm = 1 / ((1 + F.Rm) / (1 + F.inflation_smoothed))
keep year Qd Qm
tempfile temp_Q
save `temp_Q'

* Generate dataset with government rates around the world
import excel "$MyPath/Data/Aggregate Data/IGOECD3M.xlsx", clear  sheet(Price Data) firstrow
destring * , replace
gen date = date(Date, "MDY")
format date %td
gen year = year(date)
gen Rd_world = Close/ 100
collapse (mean) Rd_world, by(year)
keep year Rd_world
merge 1:1 year using `infl', nogen
* Rd_world stops in 2018 so just extrapolate in 2019 and 2020
tsset year
replace Rd_world = Rd_world[_n-1] if missing(Rd_world)
gen Qd_world = 1 / ((1 + F.Rd_world) / (1 + F.inflation_smoothed))
keep year Qd_world
tempfile temp_Q_world
save `temp_Q_world'

* compbine all asset classes
use `temp_pde_norway', clear
merge 1:1 year using `temp_pde_world', nogen
merge 1:1 year using `temp', nogen
merge 1:1 year using `temp_pdh', nogen
merge 1:1 year using `temp_pdh_jst', nogen
merge 1:1 year using `temp_Q', nogen
merge 1:1 year using `temp_Q_world', nogen
merge 1:1 year using `infl', keep(master matched) nogen keepusing(cpi) 

tsset year

* normalize house-to-rent ratios to 27 in 2013 (this does not matter for anything except for the plot of yields Figure 3)
foreach suffix in ""  _jst {
	sum PDh`suffix' if year == 2013
	replace PDh`suffix' = PDh`suffix' / `=r(mean)' * 27
}


* create deviation in asset prices
foreach v of varlist Qd Qd_world Qm PDh PDh_jst PDe PDe_world  {
	sum `v' if inrange(year, 1991, 1995)
	gen `v'0 = r(mean)
	gen `v'_shock = (`v' - `v'0) / `v'
}

* create deviation in returns (used to compute revaluation gains)
tsset year
gen reh_shock = F.PDh / PDh * F.PDh_shock / 1.05 - PDh_shock / 1.05
gen ree_shock = F.PDe / PDe * F.PDe_shock / 1.05 - PDe_shock / 1.05

* create discounting
gen R0t_adhoc = 1.05^(-(year - 1994))
keep if inrange(year, 1980, 2019)
save "$MyPath/Save/prices", replace 


/***************************************************************************************************
Figure 2: Graphical representation of the price deviation ∆PH,t
***************************************************************************************************/
use "$MyPath/Save/prices", clear
gen Ph_baseline = rentpricescpissb / cpi
sum Ph_baseline if year == 1980
replace Ph_baseline = Ph_baseline / r(mean)
gen Ph = Ph_baseline * PDh
sum Ph if year == 1980
replace Ph = Ph / r(mean)
twoway ///
(scatter Ph year, connect(l) lwidth(thick) color("black")) ///
(line Ph_baseline year, connect(l)  lwidth(thick) color("black") lpattern(dash_dot)) ///
(pcarrowi `=Ph[_N]-0.3' 2020 `=Ph_baseline[_N]+0.3' 2020, color("black")  mlabsize(large) msize(large) barbsize(medlarge)) ///
(pcarrowi `=Ph_baseline[_N]+0.3' 2020 `=Ph[_N]-0.3' 2020, color("black")  mlabsize(large) msize(large) barbsize(medlarge)) ///
, ysize(4) xsize(6) graphregion(color(white) margin(large)) ///
xlabel(, labsize(medlarge) grid glcolor(gs14) glwidth(vthin)) ///
ylabel(0 1 2 3 4 5, labsize(medlarge) grid glcolor(gs14) glwidth(vthin) angle(0))  ///
xtitle("", size(medlarge)) ///
ytitle("(1980 = 1)", size(medlarge)) ///
xscale(range(1980 2024)) ///
yscale(range(0 5)) ///
legend(off) ///
text(`=Ph[_N]' 2019.6 "P{subscript:H,t}", size(large) color("black") placement(east)) ///
text(`=Ph_baseline[_N]' 2019.4 "PD{subscript:H} x D{subscript:H,t}", size(large) color("black") placement(east)) /// 
text(`=(Ph[_N]+Ph_baseline[_N])/2' 2020.5 "{&Delta}P{subscript:H,t}", size(large) color("black") placement(east)) ///
text(`=Ph_baseline[_N]+0.2' 2019.8 "—" , size(large) color("black") placement(east))
graph export "$MyPath/Figures/Figure2.$suffix", replace

/***************************************************************************************************
Figure 3: Evolution of yields in Norway
Figure A1: Relative price deviations for four asset classes in Norway
Figure A3: Comparing our series for house-to-rent ratio with Jord` a et al. (2019)
Figure A5: Yields for domestic and foreign assets
Figure A6: Price deviations for domestic and foreign assets
***************************************************************************************************/
use "$MyPath/Save/prices", clear
keep if year >= 1990

* create yield from prices
gen Rd = 100 * (1 / Qd - 1)
gen Rd0 = 100 * (1 / Qd0 - 1)
gen Rm = 100 * (1 / Qm - 1)
gen Rm0 = 100 * (1 / Qm0 - 1)
gen DPh = 100 / PDh
gen DPh0 = 100 / PDh0
gen DPe = 100 / PDe
gen DPe0 = 100 / PDe0

twoway ///
(line DPh year, lwidth(thick) color("$blue") lpattern(dash)) ///
(line Rm year, lwidth(thick) color("$red") lpattern(shortdash)) ///
(line Rd year, lwidth(thick) color("$yellow") lpattern(longdash)) ///
(line DPe year, lwidth(thick) color("$green") lpattern(longdash_dot)) ///
(line DPh0 year if inrange(year, 1993, 1995), lwidth(medium) color("$blue") lpattern(dash)) ///
(line Rm0 year if inrange(year, 1993, 1995), lwidth(medium) color("$red") lpattern(shortdash)) ///
(line Rd0 year if inrange(year, 1993, 1995), lwidth(medium) color("$yellow") lpattern(longdash)) ///
(line DPe0 year if inrange(year, 1993, 1995), lwidth(medium) color("$green") lpattern(longdash_dot)) ///
, ysize(4) xsize(6) graphregion(color(white) margin(l=0)) ///
xlabel(, labsize(medlarge) grid glcolor(gs14) glwidth(vthin)) ///
xline(1994, lpattern(solid) lwidth(thin) lcolor(black)) ///
ylabel(0 "0%" 4 "4%" 8 "8%" 12 "12%", labsize(medlarge) grid glcolor(gs14) glwidth(vthin) angle(0)) ///
xtitle("", size(medlarge)) ///
ytitle("Yield", size(medlarge)) ///
legend(order(1 "Housing" 2 "Debt" 3 "Deposits" 4 "Equity") size(medlarge) rows(1) symysize(6) symxsize(8) region(lwidth(vvthin)))
graph export "$MyPath/Figures/Figure3.$suffix", replace

* Numbers used in the second-to-last paragraph of Section 2.2 "Aggregate data on valuations"
tempname handle
file open `handle' using "$MyPath/numbers/stat_yield_debt.tex", write replace
sum Rm if year >= 1994
local v1 = r(mean)
sum Rm0 if year >= 1994
local v2 = r(mean)
file write `handle' "`:di %3.1fc `v2' - `v1''"
file close `handle'

tempname handle
file open `handle' using "$MyPath/numbers/stat_yield_deposits.tex", write replace
sum Rd if year >= 1994
local v1 = r(mean)
sum Rd0 if year >= 1994
local v2 = r(mean)
file write `handle' "`:di %3.1fc `v2' - `v1''"
file close `handle'

tempname handle
file open `handle' using "$MyPath/numbers/stat_yield_housing.tex", write replace
sum DPh if year >= 1994
local v1 = r(mean)
sum DPh0 if year >= 1994
local v2 = r(mean)
file write `handle' "`:di %3.1fc `v2' - `v1''"
file close `handle'

tempname handle
file open `handle' using "$MyPath/numbers/stat_yield_equity.tex", write replace
sum DPe if year >= 1994
local v1 = r(mean)
sum DPe0 if year >= 1994
local v2 = r(mean)
file write `handle' "`:di %3.1fc `v2' - `v1''"
file close `handle'


* Figure A1a
twoway ///
(line PDh_shock year if year >= 1994, lwidth(thick) color("$blue") lpattern(dash)) ///
(line PDe_shock year if year >= 1994, lwidth(thick) color("$green") lpattern(longdash_dot)) ///
, yline(0, lpattern(solid) lwidth(thin) lcolor(black)) ///
ysize(4) xsize(6) graphregion(color(white) margin(l=0)) ///
xlabel(, labsize(medlarge) grid glcolor(gs14) glwidth(vthin)) ///
ylabel(, labsize(medlarge) grid glcolor(gs14) glwidth(vthin) angle(0)) ///
xtitle("", size(medlarge)) ///
ytitle("Relative price deviation", size(medlarge)) ///
legend(order(1 "Housing" 2 "Equity") size(medlarge) rows(1) symysize(6) symxsize(8) region(lwidth(vvthin)))
graph export "$MyPath/Figures/FigureA1a.$suffix", replace

* Figure A1b
twoway ///
(line Qm_shock year if year >= 1994, lwidth(thick) color("$red") lpattern(shortdash)) ///
(line Qd_shock year if year >= 1994, lwidth(thick)  color("$yellow") lpattern(longdash)) ///
, yline(0, lpattern(solid) lwidth(thin) lcolor(black)) ///
ysize(4) xsize(6) graphregion(color(white) margin(l=0)) ///
xlabel(, labsize(medlarge) grid glcolor(gs14) glwidth(vthin))  xtitle("") ///
ylabel(, labsize(medlarge) grid glcolor(gs14) glwidth(vthin) angle(0)) ///
xtitle("", size(medlarge)) ///
ytitle("Relative price deviation", size(medlarge)) ///
legend(order(1 "Debt" 2 "Deposits") size(medlarge) rows(1) symysize(6) symxsize(8) region(lwidth(vvthin)))
graph export "$MyPath/Figures/FigureA1b.$suffix", replace

* Figure A3a
gen DPh_jst = 100 / PDh_jst
twoway ///
(line DPh year if year >= 1990, lwidth(thick) color("$blue") lpattern(dash)) ///
(line DPh_jst year if year >= 1990, lwidth(medthick) color("$blue%50") lpattern(dash)) ///
, yline(0, lpattern(solid) lwidth(thin) lcolor(black)) ///
ysize(4) xsize(6) graphregion(color(white) margin(l=0)) ///
xlabel(, labsize(medlarge) grid glcolor(gs14) glwidth(vthin)) ///
xline(1994, lpattern(solid) lwidth(thin) lcolor(black)) ///
ylabel(0 "0%" 3 "3%" 6 "6%" 9 "9%" 12 "12%", labsize(medlarge) grid glcolor(gs14) glwidth(vthin) angle(0)) ///
xtitle("", size(medlarge)) ///
ytitle("Yield", size(medlarge)) ///
legend(order(1 "Baseline" 2 "Jorda et al. (2019)") size(medlarge) rows(1) symysize(6) symxsize(8) region(lwidth(vvthin)))
graph export "$MyPath/Figures/FigureA3a.$suffix", replace

* Figure A3b
twoway ///
(line PDh_shock year if year >= 1994,  lwidth(thick) color("$blue") lpattern(dash)) ///
(line PDh_jst_shock year if year >= 1994, lwidth(medthick) color("$blue%50") lpattern(dash)) ///
, yline(0, lpattern(solid) lwidth(thin) lcolor(black)) ///
ysize(4) xsize(6) graphregion(color(white) margin(l=0)) ///
xlabel(, labsize(medlarge) grid glcolor(gs14) glwidth(vthin)) ///
ylabel(, labsize(medlarge) grid glcolor(gs14) glwidth(vthin) angle(0)) ///
xtitle("", size(medlarge)) ///
ytitle("Relative price deviation", size(medlarge)) ///
legend(order(1 "Baseline" 2 "Jorda et al. (2019)") size(medlarge) rows(1) symysize(6) symxsize(8) region(lwidth(vvthin)))
graph export "$MyPath/Figures/FigureA3b.$suffix", replace

* Figure A5a
gen DPe_world = 100 / PDe_world
twoway ///
(line DPe year if year >= 1994, lwidth(thick) color("$green") lpattern(longdash_dot)) ///
(line DPe_world year if year >= 1994, lwidth(medthick)  color("$green%50") lpattern(longdash_dot)) ///
, ysize(4) xsize(6) graphregion(color(white) margin(l=0)) ///
xlabel(, labsize(medlarge) grid glcolor(gs14) glwidth(vthin))  ///
ylabel(0 "0%" 2 "2%" 4 "4%" 6 "6%", labsize(medlarge) grid glcolor(gs14) glwidth(vthin) angle(0)) ///
xtitle("") ///
ytitle("Yield", size(medlarge)) ///
legend(order(1 "Norway Equity" 2 "World equity") size(medlarge) rows(1) symysize(6) symxsize(8) region(lwidth(vvthin)))
graph export "$MyPath/Figures/FigureA5a.$suffix", replace

* Figure A5b
gen Rd_world = 100 * (1 / Qd_world - 1)
twoway ///
(line Rd year, connect(l) lwidth(thick) color("$red") lpattern(dash)) ///
(line Rd_world year, connect(l) lwidth(medthick)  color("$red%50") lpattern(dash)) ///
, ysize(4) xsize(6) graphregion(color(white) margin(l=0)) ///
xlabel(, labsize(medlarge) grid glcolor(gs14) glwidth(vthin))  ///
ylabel(-2 "-2%" 0 "0%" 2 "2%" 4 "4%" 6 "6%", labsize(medlarge) grid glcolor(gs14) glwidth(vthin) angle(0)) ///
xtitle("") ///
ytitle("Yield", size(medlarge)) ///
legend(order(1 "Norway debt" 2 "World debt") size(medlarge) rows(1) symysize(6) symxsize(8) region(lwidth(vvthin)))
graph export "$MyPath/Figures/FigureA5b.$suffix", replace

* Figure A6a
twoway ///
(line PDe_shock year if year >= 1994, lwidth(thick) color("$green") lpattern(longdash_dot)) ///
(line PDe_world_shock year if year >= 1994, lwidth(medthick)  color("$green%50") lpattern(longdash_dot)) ///
, yline(0, lpattern(solid) lwidth(thin) lcolor(black)) ///
ysize(4) xsize(6) graphregion(color(white) margin(l=0)) ///
xlabel(, labsize(medlarge) grid glcolor(gs14) glwidth(vthin))  ///
ylabel(, labsize(medlarge) grid glcolor(gs14) glwidth(vthin) angle(0)) ///
xtitle("") ///
ytitle("Relative price deviation") ///
legend(order(1 "Domestic equity" 2 "World equity") size(medlarge) rows(1) symysize(6) symxsize(8) region(lwidth(vvthin)))
graph export "$MyPath/Figures/FigureA6a.$suffix", replace

* Figure A6b
twoway ///
(line Qd_shock year if year >= 1994,  lwidth(thick) color("$red") lpattern(shortdash)) ///
(line Qd_world_shock year if year >= 1994,   lwidth(medthick)  color("$red%50") lpattern(shortdash)) ///
, yline(0, lpattern(solid) lwidth(thin) lcolor(black)) ///
ysize(4) xsize(6) graphregion(color(white) margin(l=0)) ///
xlabel(, labsize(medlarge) grid glcolor(gs14) glwidth(vthin)) ///
ylabel(, labsize(medlarge) grid glcolor(gs14) glwidth(vthin) angle(0)) ///
xtitle("") ///
ytitle("Relative price deviation") ///
legend(order(1 "Domestic debt" 2 "World debt") size(medlarge) rows(1) symysize(6) symxsize(8) region(lwidth(vvthin)))
graph export "$MyPath/Figures/FigureA6b.$suffix", replace

* Number used in Appendix C1.
gen ratio = PDh_jst_shock/PDh_shock
sum ratio
file open `handle' using "$MyPath/numbers/stat_ratio_jst.tex", write replace
file write `handle' "`:di %3.2fc r(mean)'"
file close `handle'
